package org.qloudgen.borg.pages;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.qloudgen.borg.CommonValues;
import org.qloudgen.borg.beans.DataSourceBean;
import org.qloudgen.borg.beans.FileUploadBean;
import org.qloudgen.borg.beans.FuncBean;
import org.qloudgen.borg.metadb.DefaultImpl;
import org.qloudgen.borg.pages.common.BasePage;
import org.qloudgen.borg.pages.compnent.CancelButton;
import org.qloudgen.borg.pages.panel.DSFileUploadPanel;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

@SuppressWarnings({"unchecked","rawtypes"})
public class DataSourceCreatePage extends BasePage {
	
	/**
	 * @Author youngeryoyo
	 * youngeryoyo@gmail.com
	 */
	static Log log = LogFactory.getLog(DataSourceCreatePage.class);
	
	private static final long serialVersionUID = 1L;
	private transient Map<String,List<String>> map = Maps.newHashMap();
	private transient Map<String,String> appMap = Maps.newHashMap();
	private transient Map<String,String> funcMap = Maps.newHashMap();
	private transient List<FuncBean> funcs = Lists.newArrayList();
	
	public DataSourceCreatePage(PageParameters pageParameters){
		
		super();
		
		final DSFileUploadPanel tablePanel = new DSFileUploadPanel("tablePanel","接口创建脚本：");		
		final DSFileUploadPanel loadPanel = new DSFileUploadPanel("loadPanel","数据加载脚本：" );	
		
		Form<?> form = new Form("form",new CompoundPropertyModel<DataSourceBean>(new DataSourceBean())){
			
			private static final long serialVersionUID = 1L;
			protected void onSubmit() {
				
				// get form obj
				DataSourceBean ds = (DataSourceBean)getDefaultModelObject();
				FileUploadBean table = (FileUploadBean) tablePanel.get("panel").getDefaultModelObject();
				FileUploadBean load = (FileUploadBean) loadPanel.get("panel").getDefaultModelObject();
				
				if (table.getContents() == null || load.getContents() == null ) {
					info(CommonValues.INFO_NOT_NULL);
					return;
				}
				
				ds.setTableFile(table.getContents());
				ds.setLoadFile(load.getContents());
				
				// insert into metadb
				DefaultImpl.createDataSource(ds);
				setResponsePage(DataSourceListPage.class);
			}
			
		};
		form.setOutputMarkupId(true);
		this.add(form);
		
		form.add(new RequiredTextField<String>("dsName"));
		form.add(new TextField<String>("dsDesc"));
		
		//get funcs from metadb
		DefaultImpl.getAllFuncList(funcs);
		
		//get app map from funcs
		getMap(funcs);

		IChoiceRenderer<Object> appRender = new ChoiceRenderer<Object>(){
			private static final long serialVersionUID = 1L;
			public Object getDisplayValue(Object object) {
				return appMap.get(object);
			}
		};
		
		final DropDownChoice<String> app = new DropDownChoice<String>("appNo", new ArrayList<String>(appMap.keySet()), appRender);
		app.setOutputMarkupId(true);
		app.setNullValid(false);
		app.setRequired(true);
		
		form.add(app);
		form.add(tablePanel);
		form.add(loadPanel);
		
		form.add(new Button("submit"));
		form.add(new CancelButton("cancel",DataSourceListPage.class));
		
	}

	private void getMap(List<FuncBean> funcs) {
		
		for(FuncBean func:funcs){
			
			if (map.containsKey(func.getAppNo())){
				map.get(func.getAppNo()).add(func.getFuncNo());
			} else {
				List<String> list = Lists.newArrayList();
				list.add(func.getFuncNo());
				map.put(func.getAppNo(),list );
				appMap.put(func.getAppNo(), func.getAppName());
			}
			
			funcMap.put(func.getFuncNo(), func.getFuncName());
			
		}
		
	}

}

